package com.ruihu.admin.module.system.role.roleuser;

import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.constant.SwaggerTagConst;
import com.ruihu.admin.module.system.role.basic.RoleResponseCodeConst;
import com.ruihu.admin.module.system.user.domain.vo.UserVO;
import com.ruihu.admin.module.system.role.basic.domain.dto.RoleBatchDTO;
import com.ruihu.admin.module.system.role.basic.domain.dto.RoleQueryDTO;
import com.ruihu.admin.module.system.role.basic.domain.dto.RoleSelectedVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户角色管理路由
 *
 * @author listen
 * @date 2017/12/28 10:10
 */
@Api(tags = {SwaggerTagConst.Admin.MANAGER_ROLE_USER})
@OperateLog
@RestController
public class RoleUserController {

    @Autowired
    private RoleUserService roleUserService;

    @ApiOperation(value = "获取角色成员-员工列表", notes = "获取角色成员-用户列表（分页）")
    @PostMapping("/role/listUser")
    public ResponseDTO<PageResultDTO<UserVO>> listUserByName(@Valid @RequestBody RoleQueryDTO queryDTO) {
        return ResponseDTO.succData(roleUserService.listUserByName(queryDTO));
    }

    @ApiOperation(value = "根据角色id获取角色用户列表(无分页)", notes = "根据角色id获取角色成员-用户列表")
    @GetMapping("/role/listAllUser/{roleId}")
    public ResponseDTO<List<UserVO>> listAllUserRoleId(@PathVariable Long roleId) {
        return ResponseDTO.succData(roleUserService.getAllUserByRoleId(roleId));
    }

    @ApiOperation(value = "根据角色id获取角色用户列表分页", notes = "根据角色id获取角色成员-用户列表")
    @PostMapping("/role/listUserByPage")
    public ResponseDTO<PageResultDTO<UserVO>> listAllUserRoleId(@Valid @RequestBody RoleQueryDTO queryDTO) {
        return ResponseDTO.succData(roleUserService.getUserPageByRoleId(queryDTO));
    }

    @ApiOperation(value = "从角色成员列表中移除员工", notes = "从角色成员列表中移除员工")
    @ApiImplicitParams({@ApiImplicitParam(name = "UserId", value = "员工id", paramType = "query", required = true),
            @ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query",
        required = true)})
    @GetMapping("/role/removeUser")
    public ResponseDTO<String> removeUser(Long userId, Long roleId) {
        if (null == userId || null == roleId) {
            return ResponseDTO.wrap(RoleResponseCodeConst.ERROR_PARAM);
        }
        roleUserService.removeUserRole(userId, roleId);
        return ResponseDTO.succ();
    }

    @ApiOperation(value = "从角色成员列表中批量移除员工", notes = "从角色成员列表中批量移除员工")
    @PostMapping("/role/removeUserList")
    public ResponseDTO<String> removeUserList(@Valid @RequestBody RoleBatchDTO removeDTO) {
        roleUserService.batchRemoveUserRole(removeDTO);
        return ResponseDTO.succ();
    }

    @ApiOperation(value = "角色成员列表中批量添加员工", notes = "角色成员列表中批量添加员工")
    @PostMapping("/role/addUserList")
    public ResponseDTO<String> addUserList(@Valid @RequestBody RoleBatchDTO addDTO) {
        roleUserService.batchAddRoleUser(addDTO);
        return ResponseDTO.succ();
    }

    @ApiOperation(value = "通过员工id获取所有角色以及员工具有的角色", notes = "通过员工id获取所有角色以及员工具有的角色")
    @GetMapping("/role/getRoles/{UserId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "UserId", value = "员工id", paramType = "path", required = true)})
    public ResponseDTO<List<RoleSelectedVO>> getRoleByUserId(@PathVariable Long UserId) {
        return ResponseDTO.succData(roleUserService.getRolesByUserId(UserId));
    }
}
